package com.taru.university.service.impl;

import com.taru.university.mapper.CommentMapper;
import com.taru.university.mapper.UserMapper;
import com.taru.university.model.Comment;
import com.taru.university.model.User;
import com.taru.university.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    /**
     * 查询评论
     * @param articleId
     * @return
     */
    @Override
    public List<Comment> queryCommentByArticleId(String articleId) {
        List<Comment> list = commentMapper.queryCommentByArticleId(articleId);
        for(Comment commentList : list){
            String userId = commentList.getUserId();
            User user = userMapper.queryUserById(userId);
            commentList.setUser(user);
        }
        return list;
    }

    /**
     * 插入评论
     * @param comment
     */
    @Override
    public void insertComment(Comment comment) {
        commentMapper.insertComment(comment);
    }

    /**
     * 删除评论
     * @param userId
     * @param articleId
     */
    @Override
    public void deleteComment(String userId, String articleId) {
        commentMapper.deleteComment(userId,articleId);
    }
}
